package com.by.controller;

import cn.hutool.core.bean.BeanUtil;

import com.by.bean.Products;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;

import com.by.common.group.ISave;
import com.by.common.group.V;
import com.by.query.ProductQuery;
import com.by.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author Hzy
 * @version 1.0
 * @date 2025/2/18  19:44
 * @description
 * @since jdk11
 */
@RestController
@RequestMapping("/api")
@Api(tags = "产品管理")
public class ProductController extends BaseController {

    @Autowired
    ProductService productService;

    @ApiOperation("查询产品")
    @GetMapping("/products/{id}")
    public ResultBean get(@PathVariable String id) {

        // 调用 Service 层方法获取产品列表
        Products products = productService.get(id);

        return success("产品详情", products);
    }

    @ApiOperation("保存产品")
    @PostMapping("/products")
    public ResultBean save(@Validated(ISave.class) @RequestBody Products products, BindingResult result) {
        V.validate(result);
        int rs = productService.save(products);
        return saveResult(rs);
    }

    @ApiOperation("查询产品列表")
    @PostMapping("/products/list")
    public ResultBean<PageBean<List<Products>>> queryByMap(@RequestBody ProductQuery query) {
        PageBean bean = productService.queryByMap(BeanUtil.beanToMap(query));
        return success("产品列表", bean);
    }

}
